/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERNP_MESSAGE_HPP
#define XERNP_MESSAGE_HPP

#include <map>
#include "Xern/DataStream.hpp"
#include "IMessage.hpp"
#include "IMessageDecoder.hpp"

namespace XernProtocol
{
	enum MessageType
	{
		// Nop message.
		MT_NOP = 0,

		// Session messages.
		MT_SESSION_PING_REQUEST,
		MT_SESSION_PING_RESPONSE,

		// Entity messages
		MT_ENTITY_ADDED,
		MT_ENTITY_REMOVED,
		MT_ENTITY_MOVEMENT_UPDATES,
		MT_ENTITY_ADDED_RESOURCE,
		MT_ENTITY_PHYSIC_UPDATES,
		MT_ENTITY_REMOVED_RESOURCE,

		// Player messages.
		MT_PLAYER_ENTITY_REQUEST,
		MT_PLAYER_ENTITY_RESPONSE,
		MT_PLAYER_ENTITY_EVENT,

		// Region messages.
		MT_REGION_LIST_REQUEST,
		MT_REGION_LIST_RESPONSE,
		MT_REGION_FULL_UPDATE_REQUEST,

		// Resource messages.
		MT_RESOURCE_REQUEST,
		MT_RESOURCE_REQUEST_DATA,
		MT_RESOURCE_DATA,
		MT_RESOURCE_DESCRIPTION,
		MT_RESOURCE_DENIAL,
		MT_RESOURCE_STATE,

	};

	/**
	 * The base class for xern messages.
	 */
	class Message: public IMessage
	{
	public:
		Message();
		~Message();

		virtual u8 GetFlags() const;
		virtual u8 GetChannel() const;

		virtual void WriteToPacket(Packet *packet);

		virtual MessageType GetType() const = 0;
		virtual size_t GetSize() = 0;

		virtual void Decode(DataStream &in, size_t size);
		virtual void Encode(DataStream &out) const;

	};

	/**
	 * Xern message factory base.
	 */
	class BaseMessageFactory
	{
	public:
		virtual boost::shared_ptr<Message> CreateMessage() = 0;
	};

	/**
	 * Xern message factory template.
	 */
	template<typename T>
	class MessageFactory: public BaseMessageFactory
	{
	public:
		virtual boost::shared_ptr<Message> CreateMessage()
		{
			boost::shared_ptr<Message> message(new T());
			return message;
		}

	};

	/**
	 * Xern message decoder.
	 */
	class MessageDecoder: public IMessageDecoder
	{
	public:
		MessageDecoder();
		~MessageDecoder();

		boost::shared_ptr<IMessage> DecodeMessage(Packet *packet);

	private:
		void BuildMessageTable();

		template<typename T>
		void AddMessage(u16 type)
		{
			boost::shared_ptr<BaseMessageFactory> f(new MessageFactory<T> ());
			messageTable.insert(std::make_pair(type, f));
		}

		typedef std::map<u16, boost::shared_ptr<BaseMessageFactory> > MessageTable;
		MessageTable messageTable;
	};
}; // namespace Xern

#endif //XERNP_MESSAGE_HPP
